/**
 * @fileoverview This is a file where undefinedVars checks are disabled.
 * @suppress {undefinedVars}
 */
// the above is for the Google Closure Compiler ant task

$(document).ready(function() {

    // from https://developer.mozilla.org/en/DOM/Storage, imitates localstorage with cookies if not present
    if (!window.localStorage) {
        Object.defineProperty(window, "localStorage", new (function () {
            var aKeys = [], oStorage = {};
            Object.defineProperty(oStorage, "getItem", {
                value: function (sKey) { return sKey ? this[sKey] : null; },
                writable: false,
                configurable: false,
                enumerable: false
            });
            Object.defineProperty(oStorage, "key", {
                value: function (nKeyId) { return aKeys[nKeyId]; },
                writable: false,
                configurable: false,
                enumerable: false
            });
            Object.defineProperty(oStorage, "setItem", {
                value: function (sKey, sValue) {
                    if(!sKey) { return; }
                    document.cookie = escape(sKey) + "=" + escape(sValue) + "; path=/";
                },
                writable: false,
                configurable: false,
                enumerable: false
            });
            Object.defineProperty(oStorage, "length", {
                get: function () { return aKeys.length; },
                configurable: false,
                enumerable: false
            });
            Object.defineProperty(oStorage, "removeItem", {
                value: function (sKey) {
                    if(!sKey) { return; }
                    var sExpDate = new Date();
                    sExpDate.setDate(sExpDate.getDate() - 1);
                    document.cookie = escape(sKey) + "=; expires=" + sExpDate.toGMTString() + "; path=/";
                },
                writable: false,
                configurable: false,
                enumerable: false
            });
            this.get = function () {
                var iThisIndx;
                for (var sKey in oStorage) {
                    iThisIndx = aKeys.indexOf(sKey);
                    if (iThisIndx === -1) { oStorage.setItem(sKey, oStorage[sKey]); }
                    else { aKeys.splice(iThisIndx, 1); }
                    delete oStorage[sKey];
                }
                for (aKeys; aKeys.length > 0; aKeys.splice(0, 1)) { oStorage.removeItem(aKeys[0]); }
                for (var iCouple, iKey, iCouplId = 0, aCouples = document.cookie.split(/\s*;\s*/); iCouplId < aCouples.length; iCouplId++) {
                    iCouple = aCouples[iCouplId].split(/\s*=\s*/);
                    if (iCouple.length > 1) {
                        oStorage[iKey = unescape(iCouple[0])] = unescape(iCouple[1]);
                        aKeys.push(iKey);
                    }
                }
                return oStorage;
            };
            this.configurable = false;
            this.enumerable = true;
        })());
    }

//    var id = localStorage.getItem("id");
//    if (id === null) {
//        window.location.replace("login.jsp");
//        console.log("index.js: going to login");
//    } else {
//        window.location.replace("lobby?id=" + id);
//        console.log("index.js: going to lobby");
//    }

    
//	$.ajax({
//	type: 'GET',
//	url: "/authent",
//	dataType: "text",
//	success: function(data){
//		alert(data);
//		//myData.gameID= data;
//		//console.log(myData.gameID);
//	}
//});

    if (localStorage.getItem("id") === null && localStorage.getItem("token") === null){
        $.get("authent",
            function(data) {
                window.location.replace("login.jsp");
            });
    }else{
        window.location.replace("lobby.jsp?token=" +localStorage.getItem("token"));
    }

//    $.get("authent",
//        function(data) {
//    	    window.location.replace("login.jsp");
//        });

});